/**
 * Copyright 2024 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "kernel/ascend/aclnn/kernel_mod_impl/customize/transpose_ext_aclnn_kernel.h"
#include <algorithm>
#include <vector>
#include <map>
#include <memory>
#include <functional>
#include "ir/tensor.h"
#include "kernel/ascend/acl_ir/acl_helper.h"
#include "kernel/ascend/acl_ir/op_api_convert.h"
#include "abstract/ops/primitive_infer_map.h"

namespace mindspore {
namespace kernel {
namespace transpose_ext {
void TransposeExtAscend::GetWorkSpaceInfo(const std::vector<KernelTensor *> &inputs,
                                          const std::vector<KernelTensor *> &outputs) {
  auto input_shape = inputs[kIndex0]->GetShape()->GetShapeVector();
  if (input_shape.size() == 0) {
    GetWorkspaceForResize(inputs[kIndex0], input_perm_, outputs[kIndex0]);
    return;
  }
  auto dim0 = inputs[kIndex1]->GetValueWithCheck<int64_t>();
  auto dim1 = inputs[kIndex2]->GetValueWithCheck<int64_t>();
  auto rank = SizeToLong(input_shape.size());
  dim0 = (dim0 < 0) ? (dim0 + rank) : dim0;
  dim1 = (dim1 < 0) ? (dim1 + rank) : dim1;
  input_perm_.resize(input_shape.size());
  for (size_t i = 0; i < input_perm_.size(); i++) {
    input_perm_[i] = i;
  }
  std::swap(input_perm_[dim0], input_perm_[dim1]);
  GetWorkspaceForResize(inputs[kIndex0], input_perm_, outputs[kIndex0]);
}

bool TransposeExtAscend::Launch(const std::vector<KernelTensor *> &inputs, const std::vector<KernelTensor *> &workspace,
                                const std::vector<KernelTensor *> &outputs, void *stream_ptr) {
  MS_EXCEPTION_IF_NULL(stream_ptr);
  RunOp(stream_ptr, workspace, inputs[kIndex0], input_perm_, outputs[kIndex0]);
  return true;
}

MS_ACLNN_KERNEL_FACTORY_REG(TransposeExt, TransposeExtAscend);
}  // namespace transpose_ext
}  // namespace kernel
}  // namespace mindspore
